      SUBROUTINE regularizaL2Sref(X,iflag)	

      !termo de regularizacao: L2 regularizer

      use globais
      implicit none


      integer :: i,iflag
      double precision ::X(N)
!       double precision, dimension(:,:), allocatable:: L
!       double precision, dimension(:), allocatable:: Y
!       double precision ::DNRM2

      double precision, dimension(:), allocatable :: Ux,Uz
      double precision:: aux
      double precision:: deltan2
      ! dá pra fazer sem montar a matriz.... só somando direto o produto.... 
!       allocate(L(N,N))
!       L = 0.d0 ! zera a matriz


      allocate(Ux(N))
      allocate(Uz(N))


      if(iflag.eq.1)then !calcula  Treg
	  ! operador d/dx aplicado a diferenca entre x e xref   ou xn e xref
	  do i=1,N
	      if(mod(i,Nx0).eq.1)then
		  Ux(i)  = -1.0d0*( X(i)) +1.0d0*( X(i+1))
	      elseif(mod(i,Nx0).eq.0)then
		  Ux(i)  = 1.0d0*( X(i)) -1.0d0*( X(i-1))
	      else
		  Ux(i)  = 0.5d0*( X(i+1)) -0.5d0*( X(i-1))
	      endif
	  enddo

	  ! operador d/dz aplicado a diferenca entre x e xref   ou xn e xref
	  do i=1,N
	      if(i.le.Nx0)then
		  Uz(i)  = -1.0d0*( X(i)) +1.0d0*(X(i+Nx0))
	      elseif(i.gt.(N-Nx0))then
		  Uz(i)  =  1.0d0*( X(i)) -1.0d0*( X(i-Nx0))
	      else
		  Uz(i)   = -0.5d0*( X(i-Nx0)) +0.5d0*( X(i+Nx0))
	      endif
	  enddo




	  ! L2 regularizer

	  TReg = 0.0d0
	  do i=1,N
	      TReg = TReg + Ux(i)**2 + Uz(i)**2 
	  enddo



      elseif(iflag.eq.2)then !calcula  Treg e grad

	  ! operador d/dx aplicado a diferenca entre x e xref   ou xn e xref
	  do i=1,N
	      if(mod(i,Nx0).eq.1)then
		  Ux(i)  = -1.0d0*( X(i)) +1.0d0*( X(i+1))
		  gradTReg(i  ) = gradTreg(i  ) + 2.0d0*Ux(i)*(-1.0d0)  
		  gradTReg(i+1) = gradTreg(i+1) + 2.0d0*Ux(i)*(+1.0d0)  

	      elseif(mod(i,Nx0).eq.0)then
		  Ux(i)  = 1.0d0*( X(i)) -1.0d0*( X(i-1))
		  gradTReg(i  ) = gradTreg(i  ) + 2.0d0*Ux(i)*(+1.0d0)  
		  gradTReg(i-1) = gradTreg(i-1) + 2.0d0*Ux(i)*(-1.0d0)  

	      else
		  Ux(i)  = 0.5d0*( X(i+1)) -0.5d0*( X(i-1))
		  gradTReg(i-1) = gradTreg(i-1) + 2.0d0*Ux(i)*(-0.5d0)  
		  gradTReg(i+1) = gradTreg(i+1) + 2.0d0*Ux(i)*(+0.5d0)  

	      endif
	  enddo

	  ! operador d/dz aplicado a diferenca entre x e xref   ou xn e xref
	  do i=1,N
	      if(i.le.Nx0)then
		  Uz(i)  = -1.0d0*( X(i)) +1.0d0*(X(i+Nx0))
		  gradTReg(i    ) = gradTreg(i    ) + 2.0d0*Uz(i)*(-1.0d0)  
		  gradTReg(i+Nx0) = gradTreg(i+Nx0) + 2.0d0*Uz(i)*(+1.0d0)  

	      elseif(i.gt.(N-Nx0))then
		  Uz(i)  =  1.0d0*( X(i)) -1.0d0*( X(i-Nx0))
		  gradTReg(i    ) = gradTreg(i    ) + 2.0d0*Uz(i)*(+1.0d0)  
		  gradTReg(i-Nx0) = gradTreg(i-Nx0) + 2.0d0*Uz(i)*(-1.0d0)  

	      else
		  Uz(i)   = -0.5d0*( X(i-Nx0)) +0.5d0*( X(i+Nx0))
		  gradTReg(i-Nx0) = gradTreg(i-Nx0) + 2.0d0*Uz(i)*(-0.5d0)  
		  gradTReg(i+Nx0) = gradTreg(i+Nx0) + 2.0d0*Uz(i)*(+0.5d0)  

	      endif
	  enddo






	  ! L2 regularizer
	  TReg = 0.0d0
	  do i=1,N
	      TReg = TReg + Ux(i)**2 + Uz(i)**2  
	  enddo


          do i=1,N
	      gradTReg(i) = gradTreg(i)
          enddo





      endif

	

 
      deallocate(Ux)
      deallocate(Uz)


      return
      end subroutine